package com.unnison.demo.client.jsonserializer;

import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.inject.Inject;

import com.unnison.demo.client.jsonserializer.HasStyleName;
import com.unnison.demo.client.jsonserializer.JsonSerializerPresenter;
import com.unnison.demo.client.jsonserializer.Person;
import com.unnison.demo.client.jsonserializer.JsonSerializerPresenter.CustomStyle;
import com.unnison.demo.client.jsonserializer.JsonSerializerPresenter.JsonSerializerBinder;
import com.unnison.demo.client.jsonserializer.JsonSerializerPresenter.PersonSerializer;
import com.unnison.demo.client.main.MainPresenter;
import com.unnison.framework.junit.GuitTest;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import java.util.Date;

public class TestJsonSerializerPresenter extends GuitTest {

    @Inject
    JsonSerializerPresenter presenter;

    @Override
    protected void configure() {
        mock(JsonSerializerBinder.class);
        mock(MainPresenter.class);
        mock(HasText.class);
        mock(HasValue.class);
        mock(CustomStyle.class);
        mock(HasStyleName.class);
        bind(PersonSerializer.class).toInstance(new PersonSerializer() {
            @Override
            public Person deserialize(String json) {
                return new Person();
            }

            @Override
            public String serialize(Person t) {
                return null;
            }
        });
    }

    @Before
    public void setUp() {
        presenter.go(null);
    }

    @Test
    public void testJsonValueChange() {
        presenter.data = null;
        presenter.json.setText("");
        presenter.json$valueChange();
        Assert.assertNotNull(presenter.data);
    }

    @Test
    public void testValueChange() {
        presenter.name.setText("name");
        presenter.address.setText("address");
        presenter.id.setText("123");
        Date birthday = new Date();
        presenter.birthday.setValue(birthday);
        presenter.id$name$address$birthday$valueChange();
        Person person = presenter.data;
        Assert.assertEquals(Long.parseLong("123"), (long) person.getId());
        Assert.assertEquals("name", person.getName());
        Assert.assertEquals(birthday, person.getBirthday());
        Assert.assertEquals("address", person.getAddress());
    }
}
